﻿using System;
using System.ComponentModel;
using System.Data;
using System.Web.UI.WebControls.WebParts;

namespace AdvancedLookup.AdvancedLookupDialogWebPart
{
    [ToolboxItemAttribute(false)]
    public class AdvancedLookupDialogWebPart : WebPart
    {
        private const string AscxPath = @"~/_CONTROLTEMPLATES/AdvancedLookup/AdvancedLookupDialogWebPart/AdvancedLookupDialogWebPartUserControl.ascx";

        [WebBrowsable(true),
          WebDisplayName("Value field name"),
          WebDescription(""),
          Personalizable(PersonalizationScope.Shared),
          Category("Settings"),
          DefaultValue("")]
        public string ValueFieldName { get; set; }

        [WebBrowsable(true),
        WebDisplayName("Title field name"),
        WebDescription(""),
        Personalizable(PersonalizationScope.Shared),
        Category("Settings"),
        DefaultValue("")]
        public string TitleFieldName { get; set; }

        private IWebPartRow _provider;

        private DataRowView _dataRow;

        private AdvancedLookup.AdvancedLookupDialogWebPart.AdvancedLookupDialogWebPartUserControl _advancedLookupDialogWebPartUserControl;

        private void GetRowData(object rowData)
        {
            try
            {
                _dataRow = (DataRowView)rowData;
            }
            catch
            {
                _dataRow = null;
            }

            //more wired
            SetFields();

            if (_advancedLookupDialogWebPartUserControl != null)
            {
                _advancedLookupDialogWebPartUserControl.TitleFieldName = _titleFieldName;

                _advancedLookupDialogWebPartUserControl.ValueFieldNameValue = _valueFieldNameValue;
            }
        }

        [ConnectionConsumer("Row")]
        public void SetConnectionInterface(IWebPartRow provider)
        {
            _provider = provider;

            if (_provider != null)
            {
                _provider.GetRowData(GetRowData);
            }
        }

        public void GetRowData(RowCallback callback)
        {
            callback.Invoke(this);
        }

        private string _valueFieldNameValue = string.Empty;

        private string _titleFieldName = string.Empty;

        private void SetFields()
        {
            try
            {
                //PropertyDescriptorCollection schemaProps = _provider.Schema;
                // if (schemaProps != null && schemaProps.Count > 0 && _dataRow != null)
                if (_dataRow != null && !string.IsNullOrEmpty(ValueFieldName) && !string.IsNullOrEmpty(TitleFieldName))
                {
                    DataRow providerRow = _dataRow.Row;

                    _valueFieldNameValue = providerRow[ValueFieldName] as string;

                    _titleFieldName = providerRow[TitleFieldName] as string;
                }
            }
            catch (Exception)
            {
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            //wired
            //SetFields();

            if (_advancedLookupDialogWebPartUserControl != null)
            {
                _advancedLookupDialogWebPartUserControl.TitleFieldName = _titleFieldName;

                _advancedLookupDialogWebPartUserControl.ValueFieldNameValue = _valueFieldNameValue;
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            //wired
            //SetFields();

            _advancedLookupDialogWebPartUserControl = Page.LoadControl(AscxPath) as AdvancedLookup.AdvancedLookupDialogWebPart.AdvancedLookupDialogWebPartUserControl;

            if (_advancedLookupDialogWebPartUserControl != null)
            {
                _advancedLookupDialogWebPartUserControl.TitleFieldName = _titleFieldName;

                _advancedLookupDialogWebPartUserControl.ValueFieldNameValue = _valueFieldNameValue;

                Controls.Add(_advancedLookupDialogWebPartUserControl);
            }
        }
    }
}

